package com.example.base.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.base.activity.CollectActivity;
import com.example.base.activity.HistoryActivity;
import com.example.base.activity.LoginActivity;
import com.example.base.bean.Mine;
import com.example.base.adapter.MineAdapter;
import com.example.base.R;

import java.util.ArrayList;
import java.util.List;

import static androidx.constraintlayout.motion.utils.Oscillator.TAG;


public class MineFragment extends Fragment {

  // TODO: Rename parameter arguments, choose names that match

  private static final String ARG_PARAM1 = "param1";
  private static final String ARG_PARAM2 = "param2";
  private ListView mListView;
  private View view;
  private ArrayAdapter<Mine> adapter;
  private List<Mine> objects;
  private Context context;
  private TextView textView;

  public MineFragment() {

  }


  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
  }

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
    View inflate = inflater.inflate(R.layout.fragment_mine, container, false);
    initView(inflate);
    initListener();
    initText();
    return inflate;
  }

  private void initText() {
    SharedPreferences sp2 = getActivity().getSharedPreferences("cookies", Context.MODE_PRIVATE);
    String loginusername = sp2.getString("loginUserName_wanandroid_com", "");
    SharedPreferences sp = getActivity().getSharedPreferences("username", Context.MODE_PRIVATE);
    String value1 = sp.getString("user", "null");
    if (loginusername != null && loginusername.length() != 0) {
      textView.setText(value1);
    }
  }

  private void initView(View inflate) {
    textView = inflate.findViewById(R.id.login_go);
    mListView = inflate.findViewById(R.id.mineListView);
    context = getContext();
  }

  private void initListener() {
    textView.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        startActivityForResult(new Intent(context, LoginActivity.class), 1);
      }
    });
    mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
      @Override
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        switch (position) {
          case 2:
            SharedPreferences sp = getActivity().getSharedPreferences("cookies", Context.MODE_PRIVATE);
            String loginusername = sp.getString("loginUserName_wanandroid_com", "");
            if (loginusername != null && loginusername.length() != 0) {
              Intent intent1 = new Intent(context, CollectActivity.class);
              startActivity(intent1);
            } else {
              Toast.makeText(context, "请先登录", Toast.LENGTH_LONG).show();
              Intent intent = new Intent(context, LoginActivity.class);
              context.startActivity(intent);
            }
            break;
          case 4:
            Intent intent = new Intent(context, HistoryActivity.class);
            startActivity(intent);
        }

      }
    });
  }

  @Override
  public void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
    if (resultCode == -1) {
      String result = data.getExtras().getString("result");
      Log.i(TAG, result);
      getActivity().runOnUiThread(new Runnable() {
        @Override
        public void run() {
          textView.setText(result);
        }
      });
    } else if (resultCode == 1) {
      String result = data.getExtras().getString("result");
      getActivity().runOnUiThread(new Runnable() {
        @Override
        public void run() {
          textView.setText(result);
        }
      });
    } else {
      Toast.makeText(context, "取消登录", Toast.LENGTH_SHORT).show();
    }
  }

  @Override
  public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    objects = new ArrayList<Mine>();
    Mine mine = new Mine();
    mine.setResourceId(R.drawable.ic_coin);
    mine.setTitle("我的积分");
    mine.setText("");
    objects.add(mine);

    mine = new Mine();
    mine.setResourceId(R.drawable.ic_share_article);
    mine.setTitle("我的分享");
    mine.setText("");
    objects.add(mine);

    mine = new Mine();
    mine.setResourceId(R.drawable.ic_collect);
    mine.setTitle("我的收藏");
    mine.setText("");
    objects.add(mine);

    mine = new Mine();
    mine.setResourceId(R.drawable.ic_read_later);
    mine.setTitle("我的书签");
    mine.setText("");
    objects.add(mine);

    mine = new Mine();
    mine.setResourceId(R.drawable.ic_read_record);
    mine.setTitle("我的历史");
    mine.setText("");
    objects.add(mine);

    mine = new Mine();
    mine.setResourceId(R.drawable.ic_github);
    mine.setTitle("开源项目");
    mine.setText("");
    objects.add(mine);
    mine = new Mine();

    mine.setResourceId(R.drawable.ic_about);
    mine.setTitle("关于作者");
    mine.setText("请他喝杯咖啡~");
    objects.add(mine);

    mine = new Mine();
    mine.setResourceId(R.drawable.ic_setting);
    mine.setTitle("系统设置");
    mine.setText("");
    objects.add(mine);

    adapter = new MineAdapter(getActivity(), R.layout.list_view_mine, objects);
    mListView.setAdapter(adapter);
  }

}